<template>
	<view class="container">
		<view class="cu-form-group margin-lr-lg border-b-line padding-0">
			<view class="title">支付宝姓名</view>
			<input focus placeholder="请填写支付宝姓名" v-model="alipay_name" />
		</view>
		<view class="cu-form-group margin-lr-lg border-b-line padding-0">
			<view class="title">支付宝帐号</view>
			<input focus placeholder="请填写支付宝帐号" v-model="alipay_account" />
		</view>
		<view class="cu-form-group margin-lr-lg padding-0 submit_btn">
			<button class="cu-btn" type="button" @click="updateData">立即绑定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				alipay_name:'',
				alipay_account:'',
				userinfo:[]
			};
		},
		onNavigationBarButtonTap(val) {
			this.updateData();
		},
		onLoad: function (option) {
			this.helper.checkLogin();
			this.getData();
		},
		methods: {
			getData:function(){
				//获取基础参数
				uni.showLoading({
					title:"数据加载中",
					mask:true
				})
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+info.username,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getUserInfo",
					timestamp:timestamp,
					data:{username:info.username,token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.userinfo = res.data.data.userinfo;
					this.alipay_name = res.data.data.userinfo.alipay_name;
					this.alipay_account = res.data.data.userinfo.alipay_account;
					setTimeout(() => {
						uni.hideLoading();
					}, 300);
				})
			},
			updateData:function(){
				if(this.alipay_name == ''){
					uni.showToast({
						title:"请输入支付宝姓名",
						icon:"none"
					})
					return;
				}
				if(this.alipay_account == ''){
					uni.showToast({
						title:"请输入支付宝账号",
						icon:"none"
					})
					return;
				}
				//获取基础参数
				uni.showLoading({
					title:"数据更新中",
					mask:true
				})
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['alipay_name='+this.alipay_name,'alipay_account='+this.alipay_account,'timestamp='+timestamp,'token='+info.token]);
				this.helper.req({
					api:"saveAlipay",
					timestamp:timestamp,
					data:{alipay_name:this.alipay_name,alipay_account:this.alipay_account,token:info.token},
					header:{sign:sign},
					method:'POST',
				}).then(res=>{
					uni.hideLoading();
					
					uni.showToast({
						title: "更新成功",
						icon: "success"
					})
				})
			},
			//链接
			toUrl:function(e){
				uni.navigateTo({
					url: e,
				});	
			},
		},
	};
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}
</style>
